package com.dispatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.dispatcher.bean.Order;
import com.dispatcher.bean.Sender;
import com.dispatcher.bean.UserOrders;
import com.dispatcher.common.CommonDefine;
import com.dispatcher.common.CommonDefine.Flag_Activity;
import com.dispatcher.helper.SDHelper;
import com.dispatcher.http.HttpHelperWraper;
import com.dispatcher.http.HttpHelperWraper.HttpRequestParam;
import com.dispatcher.http.HttpHelperWraper.OnNetListener;
import com.zuzhili.database.CacheDataCtrl;
import com.zuzhili.database.CacheDataCtrl.CacheType;
import com.zuzhili.dialog.CustomDlg;

public class ReportStatusActivity extends ActivityBase implements OnNetListener, OnClickListener {
	private Button btNotDelivered;
	private Button btPayed;
	private Button btCallPhone;
	EditText metInputSendno;
//	Intent it;
	String status; // 5 代表未妥投，2代表已支付
	final String NOT_DELIVERED = "not_delivered";
	final String CANCELED = "canceled";
	final String PAYED = "payed";
	final String SENDED = "sended";
	
	Bundle bundle = null;
	Order muserOrders;
	
	List<Sender> msenderlist;
	int mcurSelectSender=-1;
	String mcurSendNo="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_status);
		bundle = getIntent().getExtras();
		muserOrders=(Order)bundle.getSerializable("userorders");
		initViews();
		if(msenderlist==null){
			
			getSenderList();
		}
	}
	
	private void initViews() {
		btNotDelivered = (Button) findViewById(R.id.bt_not_delivered);
		btPayed = (Button) findViewById(R.id.bt_payed);
		btCallPhone = (Button) findViewById(R.id.bt_phone);
		btNotDelivered.setOnClickListener(this);
		btPayed.setOnClickListener(this);
		btCallPhone.setOnClickListener(this);
		
		Button btn = (Button) findViewById(R.id.bt_send);
		btn.setOnClickListener(this);
		
		btn = (Button) findViewById(R.id.bt_canceled);
		btn.setOnClickListener(this);
		
		btn = (Button) findViewById(R.id.bt_viewsendinfo);
		btn.setOnClickListener(this);
		
	}
	
	void sendRequest(String action) {
		HttpHelperWraper http = new HttpHelperWraper();
		HttpRequestParam param = http.getParam();
		makeparam(param, action);
		http.AsyncTask(param);
	}
	
	void getSenderList() {
		HttpHelperWraper http = new HttpHelperWraper();
		HttpRequestParam param = http.getParam();
		param.ctx = this;
		param.listener = this;
		param.activitybase=this;
		param.cachetype=CacheDataCtrl.CacheType.UnPickOrders_Cache;
		param.identify="senders";
		param.expiretime=0;
		HashMap<String, Object> requestparams = new HashMap<String, Object>();
		requestparams.put("task", "getSenderList");
		param.nodesrequest = requestparams;
		
		http.AsyncTaskWithCache(param);
	}
	
	void makeparam(HttpRequestParam param, String action){
		if (param == null) {
			return;
		}
		param.ctx = this;
		param.listener = this;
		
		HashMap<String, Object> requestparams = new HashMap<String, Object>();

		requestparams.put("task", "modifyOrderState");
		requestparams.put("presentid", SDHelper.getPresentId());
		List<Order> list=new ArrayList<Order>();
		list.add(muserOrders);
		requestparams.put("orders", JSON.toJSONString(list));
		if(action.equals(NOT_DELIVERED)) {
			status = "5";
		} else if(action.equals(PAYED)) {
			status = "2";
		} else if(action.equals(CANCELED)) {
			status = "0";
		} else if(action.equals(SENDED)) {
			status = "6";
			requestparams.put("senderid", msenderlist.get(mcurSelectSender).getSenderid());
			requestparams.put("sendno", mcurSendNo);
		}
		requestparams.put("status", status);
//		requestparams.put("giftid", it.getStringExtra("giftid"));
//		requestparams.put("goodid", it.getStringExtra("goodid"));
//		requestparams.put("goodtype", it.getStringExtra("goodtype"));
		param.nodesrequest = requestparams;
	}
	
	@Override
	public void onNetSuccess(HttpRequestParam param) {
		if(param.nodesrequest.get("task").equals("getSenderList")){
			msenderlist=JSON.parseArray(JSON.parseObject(param.jsonstr).getString("list"), Sender.class);
		}else{
			
			Log.i("liutao", param.nodesresult.toString());
			Intent data = new Intent();
			data.putExtra("orderid", muserOrders.getOrderid());
			setResult(RESULT_OK, data);
			finish();
			Toast.makeText(this, "操作成功", 2000).show();
		}
	}

	@Override
	public void onNetError(HttpRequestParam param) {
		Toast.makeText(this, param.rstmsg, 2000).show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_not_delivered:
			dialogPop(NOT_DELIVERED,"未妥投");
			break;
		case R.id.bt_canceled:
			dialogPop(CANCELED,"取消订单");
			break;
		case R.id.bt_viewsendinfo:
			Intent viewsendinfo = new Intent(this,ProductActivity.class);
			
			viewsendinfo.putExtra("sendno", muserOrders.getSendno());
			startActivity(viewsendinfo);
			break;
		case R.id.bt_payed:
			dialogPop(PAYED,"已支付");
//			sendRequest(PAYED);
			
//			Intent it2 = new Intent();
//			it2.setClass(this, HomeTabActivity.class);
//			it2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			it2.putExtra("backtohome", true);
//			startActivity(it2);
//			finish();
			break;
			
		case R.id.bt_send:
			dialogPopSelectSender();
			break;
			
		case R.id.bt_phone:
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			String phone = muserOrders.getPhone();
//			if(it != null) {
//				phone = it.getStringExtra("phone");
//			}
			
			callIntent.setData(Uri.parse("tel:" + phone));
			startActivity(callIntent);
			break;

		default:
			break;
		}
		
	}
	void dialogPop(final String typeOp,String toaststr){
		final CustomDlg cDlg = new CustomDlg(this,
				R.style.popDialog);
		cDlg.setDisplayView(null, "确定执行 "+toaststr+" ？", "确定", "取消");
		cDlg.setLBtnListner(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cDlg.cancel();
				sendRequest(typeOp);
			}
		});
		cDlg.setRBtnListner(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cDlg.cancel();
			}
		});
		cDlg.show();
	}
	void dialogPopSelectSender(){
		final CustomDlg cDlg = new CustomDlg(this,
				R.style.popDialog);
		if(msenderlist==null || msenderlist.size()==0){
			show_toast("please waiting...");
			return;
		}
		String[] items=new String[msenderlist.size()];
		int index=0;
		for(Sender item:msenderlist){
			items[index]=item.getName();
			index++;
		}
		cDlg.setList(items, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				mcurSelectSender=which;
				dialogPopInputSendno();
				dialog.cancel();
			}
		});
		cDlg.show();
	}
	void dialogPopInputSendno(){
		final CustomDlg cDlg = new CustomDlg(this,
				R.style.popDialog);
		if(msenderlist==null || msenderlist.size()==0){
			show_toast("please waiting...");
			return;
		}
		LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v=inflater.inflate(R.layout.dialog_inputsendno, null);
		metInputSendno=(EditText)v.findViewById(R.id.content);
		Button left = (Button)v.findViewById(R.id.ok);
		left.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mcurSendNo=metInputSendno.getText().toString();
				if(mcurSendNo==null || mcurSendNo.length()==0){
					show_toast("please input sendno");
				}else{
					
					sendRequest(SENDED);
					
					cDlg.cancel();
				}
			}
		});
		Button right = (Button)v.findViewById(R.id.cancel);
		right.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cDlg.cancel();
			}
		});
		Button mid = (Button)v.findViewById(R.id.scan);
		mid.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent it=new Intent(ReportStatusActivity.this,Activity_ScanQRcode.class);
				
				startActivityForResult(it, CommonDefine.Flag_Activity.Flag_Activity_ScanQrcode);
			}
		});
		cDlg.setDisplayView(v,null);
		
		cDlg.show();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode!=RESULT_OK){
			return;
		}
		if(requestCode == Flag_Activity.Flag_Activity_ScanQrcode) {
			if(data != null) {
				metInputSendno.setText(data.getStringExtra("rststr"));
			}
			
		} 
	}
}
